﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace Simhopp.Common
{
    public static class CredentialsMap
    {
        public static int SaveCredentialsToDatabase(Credentials credentials)
        {
            return GenericMap.SaveObjectToDatabase(credentials);
        }

        public static Credentials ReadCredentialsFromDataBase(int id)
        {
            var dbResult = GenericMap.ReadObjectFromDatabase(id, typeof (Credentials));
            return dbResult == null ? null : Credentials.GetFromDictionary(dbResult);
        }

        public static void RemoveCredentialsFromDatabase(Credentials credentials)
        {
            GenericMap.RemoveObjectFromDatabase(credentials);
        }

        public static List<Credentials> ReadAllCredentialsFromDataBase()
        {
            var dictionary = GenericMap.ReadAllObjectsFromDatabase(typeof(Credentials));
            try
            {
                var credentials = dictionary.Select(Credentials.GetFromDictionary).ToList();
                MyLogger.Debug("All dictionaries converted to Credentials-objects");
                return credentials;
            }
            catch (Exception e)
            {
                MyLogger.Error("Could not convert all dictionaries to Credentials-objects - " + e.Message);
                return null;
            }
        }
    }
}
